<template>
<!--发微博页面 PostView-->
  <h1>发微博页面</h1>
  <div style="width: 500px;margin: 0 auto;">
    <el-input placeholder="微博正文" v-model="weibo.content"></el-input>
    <el-button type="primary" @click="post"
               style="width: 100px;margin: 10px;font-weight: bold;">发布</el-button>
  </div>
</template>

<script setup>
  import {ref} from "vue";
  import router from "@/router";
  import qs from "qs";
  import axios from "axios";
  import {ElMessage} from "element-plus";

  const weibo = ref({content:''});
  //发微博方法
  const post = ()=>{
    //拿到用户信息,确认登录后才可以发微博
    let user = localStorage.user?JSON.parse(localStorage.user):null;
    if(user==null){
      alert('请先登录!');
      router.push('/login');
      return;
    }
    //给微博设置作者id
    weibo.value.userId= user.id;
    let data = qs.stringify(weibo.value);
    //向后端发请求新增一条微博
    axios.post('http://localhost:8080/v1/weibos/add-new',data)
    .then((response)=>{
      if(response.data.code==2001){
        ElMessage.success('发布成功!');
        router.push('/');
      }
    })
  }
</script>

<style scoped>

</style>